package com.qiyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qiyu.common.ServiceException;
import com.qiyu.dao.ResortOverviewMapper;
import com.qiyu.entity.ResortOverview;
import com.qiyu.model.request.backend.ResortOverviewReq;
import com.qiyu.service.ResortOverviewService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * 度假区概况
 *
 * @author zhangweiwei
 * @date 2020-07-30
 */
@Service
@Slf4j
public class ResortOverviewServiceImpl extends ServiceImpl<ResortOverviewMapper, ResortOverview> implements ResortOverviewService {
    @Autowired
    private ResortOverviewMapper resortOverviewMapper;
    @Override
    public ResortOverview saves(ResortOverviewReq req) {
        if(StringUtils.isEmpty(req.getHandler())){
            throw new ServiceException("请传入handler");
        }
        ResortOverview resortOverview = new ResortOverview();

        switch (req.getHandler()){
            case "query":
               resortOverview = this.query(req);
               break;
            case "update":
                resortOverview = this.update(req);
                break;
                default:
                    throw new ServiceException("handler 传值不正确，请传入 'query' 或者 'update'");
        }

        return resortOverview;
    }
    private ResortOverview query(ResortOverviewReq req){
        QueryWrapper<ResortOverview> wrapper = new QueryWrapper<>();
        wrapper.lambda().isNotNull(ResortOverview::getId);
        ResortOverview resortOverview =  this.getOne(wrapper);
        return  resortOverview;
    }
    private ResortOverview update(ResortOverviewReq req){
        ResortOverview resortOverview = this.query(req);
        if(resortOverview == null){
            resortOverview = new ResortOverview();

        }
        BeanUtils.copyProperties(req,resortOverview);
        saveOrUpdate(resortOverview);
        return this.query(req);

    }
}